const { post, get } = require('../../utils/request');
const { formatDate } = require('../../utils/util');

Page({
    data: {
        date: formatDate(new Date()),
        weight: '',
        notes: '',
        records: [],
        page: 1,
        pageSize: 20,
        hasMore: true
    },

    onLoad() {
        // 设置默认日期为今天
        const today = formatDate(new Date());
        console.log('初始化日期:', today);
        this.setData({
            date: today
        });
        this.loadRecords();
    },

    onShow() {
        this.loadRecords();
    },

    // 日期变化
    onDateChange(e) {
        console.log('日期选择变化:', e.detail.value);
        this.setData({
            date: e.detail.value
        });
    },

    // 体重输入
    onWeightInput(e) {
        this.setData({
            weight: e.detail.value
        });
    },

    // 备注输入
    onNotesInput(e) {
        this.setData({
            notes: e.detail.value
        });
    },

    // 加载记录
    async loadRecords() {
        try {
            const res = await get('/records/weight', {
                page: this.data.page,
                pageSize: this.data.pageSize
            });

            this.setData({
                records: res.records,
                hasMore: res.records.length === this.data.pageSize
            });
        } catch (error) {
            console.error('加载记录失败:', error);
            wx.showToast({
                title: '加载失败',
                icon: 'none'
            });
        }
    },

    // 提交记录
    async handleSubmit() {
        const { date, weight, notes } = this.data;
        console.log('当前表单数据:', this.data);

        if (!date) {
            console.error('日期为空');
            wx.showToast({
                title: '请选择日期',
                icon: 'none'
            });
            return;
        }

        if (!weight) {
            wx.showToast({
                title: '请输入体重',
                icon: 'none'
            });
            return;
        }

        const weightNum = parseFloat(weight);
        if (isNaN(weightNum) || weightNum <= 0 || weightNum > 300) {
            wx.showToast({
                title: '请输入有效的体重',
                icon: 'none'
            });
            return;
        }

        try {
            wx.showLoading({
                title: '保存中...',
            });

            // 构建请求数据
            const requestData = {
                date: date,
                weight: weightNum,
                notes: notes || ''
            };

            console.log('发送请求数据:', requestData);

            const res = await post('/records/weight', requestData);

            console.log('提交成功，响应数据:', res);

            wx.hideLoading();
            wx.showToast({
                title: '记录成功',
                icon: 'success'
            });

            // 重置表单并刷新记录
            this.setData({
                weight: '',
                notes: ''
            });
            this.loadRecords();

        } catch (error) {
            console.error('提交体重记录失败:', error);
            console.error('错误详情:', error.message);
            wx.hideLoading();
            wx.showToast({
                title: error.message || '记录失败',
                icon: 'none'
            });
        }
    },

    onPullDownRefresh() {
        this.setData({
            page: 1,
            hasMore: true
        });
        this.loadRecords().then(() => {
            wx.stopPullDownRefresh();
        });
    },

    onReachBottom() {
        if (this.data.hasMore) {
            this.setData({
                page: this.data.page + 1
            });
            this.loadRecords();
        }
    }
}); 